<?php
	require_once("../functions/sesion.class.php");
	require_once('../functions/connection.php');

	$sesion = new sesion();
	
	if( isset($_POST["iniciar"]) )
	{
		
		$usuario = $_POST["usuario"];
		$password = $_POST["contrasenia"];
		
		if(validarUsuario($usuario,$password) == true)
		{			
			$sesion->set("usuario",$usuario);
			
			header("location: search-notifications.php");
		}
		else 
		{
			echo "<script>alert('El nombre de usuario o la contraseña son inválidos')</script>";
		}
	}
	
	function validarUsuario($usuario, $password)
	{
		$con = connect();
		$consulta = "SELECT contrasenia FROM autorizacion WHERE nick = '$usuario';";
		
		$result = $con->query($consulta);
		
		if($result->num_rows > 0)
		{
			$fila = $result->fetch_assoc();
			if( strcmp(md5($password),$fila["contrasenia"]) == 0 )
				return true;						
			else					
				return false;
		}
		else
				return false;
	}

?>
<!DOCTYPE html>
<html lang="es">
    <head>
		<meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
		<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <title>Notification Manager Login</title>
        <link rel="stylesheet" type="text/css" href="../css/style.css" />
        <script type="text/javascript" src="../js/jquery.1.7.2.js"></script>
        <script src="../js/modernizr.custom.63321.js"></script>
        
        <!--[if lte IE 7]><style>.main{display:none;} .support-note .note-ie{display:block;}</style><![endif]-->
		<style>
			body {
				background-color: #EDEDED;
			}
		</style>
    </head>
    <body>
        <div class="container">
		
			<!-- Codrops top bar -->
			
			<header>
			
				<h1>Notification<strong>Manager</strong></h1>
				<h2>Administre las notificaciones de los dispositivos móviles</h2>
				
				<div class="support-note">
					<span class="note-ie">Intenga cargar esta pagina con otro navegador</span>
				</div>
				
			</header>
			
			<section class="main">
				<form class="form-2" name="frmLogin" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
					<h1><span class="log-in">Iniciar sesión</span></h1>
					<p class="float">
						<label for="login"><i class="icon-user"></i>Usuario</label>
						<input id="username" type="text" name="usuario" placeholder="Username">
					</p>
					<p class="float">
						<label for="password"><i class="icon-lock"></i>Contraseña</label>
						<input id="password" type="password" name="contrasenia" placeholder="Password" class="showpassword">
					</p>
					<p class="clearfix"> 
					<!-- 	<a onclick="login();" href="#" class="log-twitter">Log in</a> -->   
						<input type="submit" name="iniciar" value="Log in"> 
					</p>
				</form>​​
			</section>
			
        </div>
		<!-- jQuery if needed -->
		<script type="text/javascript">
				
			$(function(){
			    $(".showpassword").each(function(index,input) {
			        var $input = $(input);
			        $("<p class='opt'/>").append(
			            $("<input type='checkbox' class='showpasswordcheckbox' id='showPassword' />").click(function() {
			                var change = $(this).is(":checked") ? "text" : "password";
			                var rep = $("<input placeholder='Password' type='" + change + "' />")
			                    .attr("id", $input.attr("id"))
			                    .attr("name", $input.attr("name"))
			                    .attr('class', $input.attr('class'))
			                    .val($input.val())
			                    .insertBefore($input);
			                $input.remove();
			                $input = rep;
			             })
			        ).append($("<label for='showPassword'/>").text("Mostrar contraseña")).insertAfter($input.parent());
			    });

			    $('#showPassword').click(function(){
					if($("#showPassword").is(":checked")) {
						$('.icon-lock').addClass('icon-unlock');
						$('.icon-unlock').removeClass('icon-lock');    
					} else {
						$('.icon-unlock').addClass('icon-lock');
						$('.icon-lock').removeClass('icon-unlock');
					}
			    });
			});
		</script>
    </body>
</html>